//
//  StatusCell.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/21.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    var bottomTopConstraint: Constraint?
    
    // 添加模型属性
    var status:Status? {
        didSet {
            // 设置原创微博的数据模型
            statusOriginalView.status = status

            // 卸载约束对象
            self.bottomTopConstraint?.uninstall()
            // 根据数据调整约束关系
            if let re = status?.retweeted_status  {
                // 转发微博
                // 设置转发微博的数据模型
                statusRetweetedView.retweeted_status = re
                statusCellBottomView.snp_updateConstraints { (make) -> Void in
                    self.bottomTopConstraint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                }
                
                // 显示转发微博视图
                statusRetweetedView.hidden = false
                
            }else {
                // 原创微博
                statusCellBottomView.snp_updateConstraints { (make) -> Void in
                    self.bottomTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                }
            
                // 隐藏转发微博视图
                statusRetweetedView.hidden = true
            }
        }
    }
    // xib加载的视图，才会走这个方法
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    
    // 这是cell的默认的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 完成自定义的操作
        setupUI()
        
        // 设置cell选中的样式
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        //添加子视图  需要使用 contentView来进行添加
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellBottomView)
        contentView.addSubview(statusRetweetedView)
        
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            // 设置高度
            //            make.height.equalTo(80)
            
            // 方式2： 遍历约束对象查询，然后再进行约束匹配，不是特别高效
            //            make.left.right.top.equalTo(contentView)
        }
        
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
           self.bottomTopConstraint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(35)
        }
        
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.equalTo(statusOriginalView.snp_left)
            make.right.equalTo(statusOriginalView.snp_right)
        }
        
        // 给cell的contentView设置约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            // 最关键的约束
            make.bottom.equalTo(statusCellBottomView.snp_bottom)
        }
        
    }
    
    // 懒加载子视图
    private lazy var statusOriginalView: StatusCellOriginalView = StatusCellOriginalView()
    
    private lazy var statusCellBottomView: StatusCellBottomView = StatusCellBottomView()
    
    private lazy var statusRetweetedView: StatusRetweetedView = StatusRetweetedView()
}









